<?php
/**
 * Created by PhpStorm.
 * 楼小二用户
 * User: caicm
 * Date: 2017/2/18
 * Time: 9:13
 */
namespace SDXApi\Model;
use Think\Model;

class LouxiaoerUserModel extends Model
{
    /*
     * 添加楼小二用户
     * Created By caicm
     * @param int $louxiaoer_id
     * @param int $user_id
     * @param int $landmark_id
     * return bool
     */
    public function addLouxiaoerUser($louxiaoer_id, $user_id, $address_id, $landmark_id){

        $data = array(
            'landmark_id' => $landmark_id,
            'louxiaoer_id' => $louxiaoer_id,
            'user_id' => $user_id,
            'address_id' => $address_id,
            'add_time' => gmtime()
        );

        return $this->add($data);

    }

    /*
     * 查询楼小二用户
     * Created By caicm
     * @param int $louxiaoer_id
     * @param int $user_id
     * return mixed
     */
    public function findLouxiaoerUser($louxiaoer_id, $user_id, $address_id){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'address_id' => $address_id,
            'user_id' => $user_id
        );

        return $this->where($where)->find();

    }

    public function findLouxiaoerUserById($id){

        $where = array(
            'id' => $id
        );

        return $this->where($where)->find();
    }

    /*
     * 统计管理用户数量
     * Created By caicm
     * @param $louxiaoer_id 楼小二id
     * @param int $user_id
     * return number
     */
    public function findCountUser($louxiaoer_id, $user_id=0){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'user_id' => $user_id
        );

        if($user_id==0) unset($where['user_id']);

        return $this->where($where)->count();

    }




    /*
     * 清除用户绑定群组
     * Created By caicm
     * @param $louxiaoer_id 楼小二id
     * @param int $group_id 群组id
     * return mixed
     */
    public function delUserToGroup($louxiaoer_id, $group_id){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'group_id' => $group_id
        );

        $data = array(
            'group_id' => 0
        );

        return $this->where($where)->data($data)->save();

    }

    /*
     * 获取用户列表
     * Created By caicm
     * @param int $louxiaoer_id 楼小二id
     * @param int $group_id 群组id
     * @param mixed $limit 分页
     * @param string $keyword 关键词
     * @param int $type 类型（1未分配，0全部）
     * return mixed
     */
    public function findMemberList($louxiaoer_id, $group_id=0, $limit, $keyword='', $type=0){

        $where = array(
            'lu.louxiaoer_id' =>$louxiaoer_id,
            'g.group_id' => $group_id,
            'g.id' => array('exp','is NULL')
        );
        if($keyword) $where['_string'] = ' a.consignee like "%'.$keyword.'%"  OR  a.address like "%'.$keyword.'%" OR  a.mobile like "%'.$keyword.'%"  ';


        $field = "lu.id as user_id,lu.remark,a.address,a.consignee as name,a.mobile as phone,g.group_id,a.address_name,a.sign_building,concat(IFNULL(c.region_name, ''), ' ', IFNULL(p.region_name, ''), " .
            "' ', IFNULL(t.region_name, ''), ' ', IFNULL(d.region_name, '')) AS region";

        if($where['g.group_id']==0) unset($where['g.group_id']) ;
        if($type==0) unset($where['g.id']) ;

        return
            $this->alias('lu')
                ->field($field)
                ->join('left join sdx_louxiaoer_group_user g on lu.id=g.louxiaoer_user_id')
                ->join("left join sdx_order_info a on lu.address_id=a.address_id and lu.user_id=a.user_id")
                ->join('left join sdx_region c ON a.country = c.region_id')
                ->join('left join sdx_region p ON a.province = p.region_id')
                ->join('left join sdx_region t ON a.city = t.region_id')
                ->join('left join sdx_region d ON a.district = d.region_id')
                ->where($where)
                ->group('lu.id')
                ->limit($limit)
                ->order('lu.id desc')
                ->select();

    }

    /*
     * 计算用户数量
     * Created By caicm
     * @param int $louxiaoer_id 楼小二id
     * @param int $group_id 群组id
     * @param mixed $limit 分页
     * @param string $keyword 关键词
     * @param int $type 类型（1未分配，0全部）
     * return number
     */
    public function findMemberCount($louxiaoer_id, $group_id=0, $keyword='', $type=0){

        $where = array(
            'lu.louxiaoer_id' =>$louxiaoer_id,
            'g.group_id' => $group_id,
            'g.id' => array('exp','is NULL')
        );
        if($keyword) $where['_string'] = ' a.consignee like "%'.$keyword.'%"  OR  a.address like "%'.$keyword.'%" OR  a.mobile like "%'.$keyword.'%"  ';

        if($where['g.group_id']==0) unset($where['g.group_id']) ;
        if($type==0) unset($where['g.id']) ;


        return  $this->alias('lu')
                ->join('left join sdx_louxiaoer_group_user g on lu.id=g.louxiaoer_user_id')
                ->join("left join sdx_order_info a on lu.address_id=a.address_id and lu.user_id=a.user_id")
                ->join('left join sdx_region c ON a.country = c.region_id')
                ->join('left join sdx_region p ON a.province = p.region_id')
                ->join('left join sdx_region t ON a.city = t.region_id')
                ->join('left join sdx_region d ON a.district = d.region_id')
                ->where($where)
                ->count("DISTINCT lu.id");

    }

    /*
     * 添加会员备注
     * Created By caicm
     * @param $louxiaoer_id 楼小二id
     * @param int $member_id 会员id
     * return mixed
     */
    public function addRemark($louxiaoer_id, $member_id, $remark){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'id' => $member_id
        );

        $data = array(
            'remark' => $remark,
            'update_time' => gmtime()
        );

        return $this->where($where)->data($data)->save();

    }



}
